package com.simple.datax.db.service.impl;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.SimpleMessage;
import com.simple.datax.SimpleObject;
import com.simple.datax.api.SimpleException;
import com.simple.datax.service.DBDataxStatusService;
import com.simple.datax.service.SimpleDBException;
import com.simple.datax.utils.SimpleConstants;

/**
 * @Title: DBDataxStatusServiceImpl.java
 * @Package com.simple.datax.db.service.impl
 * @Description: TODO(添加描述)
 * @author Paladin Chen
 * @date 2012-12-27 上午11:05:15
 * @version V1.0
 */
public class DBDataxStatusServiceImpl extends AbstractDBServiceImpl implements DBDataxStatusService {
	private static final Logger logger = LoggerFactory.getLogger(DBDataxStatusServiceImpl.class);
	/**
	 * 更新状态类型：1、原始接收状态；2、解析后状态；3、发送状态
	 */
	private int backupType = 1;

	public int getBackupType() {
		return backupType;
	}

	public void setBackupType(int backupType) {
		this.backupType = backupType;
	}

	@Override
	public void process(SimpleObject src) throws SimpleException {
		if (src instanceof SimpleMessage) {
			logger.debug("Begin to db status proc......type:" + backupType + " " + ((SimpleMessage) src).toLog());

			switch (backupType) {
			case 2:
				try {
					updateProcing(((SimpleMessage) src).getRecvRawMsgid(), SimpleConstants.STATUS_PARSE);
				} catch (Exception e) {
					logger.error(((SimpleMessage) src).toLog() + "Update DataxProcing Error:", e);
				}
				break;

			case 3:
				try {
					procSent((SimpleMessage) src);
				} catch (Exception e) {
					logger.error(((SimpleMessage) src).toLog() + "Proc Sent Status Error:", e);
				}
				break;

			default:
				try {
					insertProcing((SimpleMessage) src);
				} catch (Exception e) {
					logger.error(((SimpleMessage) src).toLog() + "Insert DataxProcing Error:", e);
				}
				break;
			}
			logger.debug("Finish db Status proc......type:" + backupType + " " + ((SimpleMessage) src).toLog());
		} else {
			//logger.error("This function Only proc SimpleMessage!!");
			throw new SimpleDBException("The Parameter is NOT SimpleMessage!!");
		}
	}

}
